40.4.3 OutputCapture

OutputCapture是JUnit的一个Rule,用于捕获System.outSystem.err输出,只需简单的将@Rule注解capture,然后在断言中调用toString()

import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.OutputCapture;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

public class MyTest {
@Rule
public OutputCapture capture = new OutputCapture();
@Test
public void testName() throws Exception {
System.out.println("Hello World!");
assertThat(capture.toString(), containsString("World"));
}
}
powered by Gitbook该页面构建时间: 2017-08-01 08:30:17

results matching ""

    No results matching ""